package com.ly.heart.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.FileEntity;
import com.ly.heart.domain.SurveyBaseDataEntity;
import com.ly.heart.domain.SurveyDataDictEntity;
import com.ly.heart.domain.SurveyElectricEntity;
import com.ly.heart.domain.dto.SurveyDataDto;
import com.ly.heart.mapper.SurveyBaseDataMapper;
import com.ly.heart.mapper.SurveyDataDictMapper;
import com.ly.heart.mapper.SurveyElectricMapper;
import com.ly.heart.service.IFileInfoService;
import com.ly.heart.service.ISurveyBaseDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 勘测基础数据采集Service业务层处理
 * @author wutao
 * @date 2024-05-22
 */
@Service
@Slf4j
public class SurveyBaseDataServiceImpl extends ServiceImpl<SurveyBaseDataMapper, SurveyBaseDataEntity> implements ISurveyBaseDataService {

    @Resource
    private SurveyBaseDataMapper erpSurveyDataCollectInfoMapper;
    @Resource
    private IFileInfoService iFileInfoService;
    @Resource
    private SurveyElectricMapper surveyElectricMapper;
    @Resource
    private SurveyDataDictMapper surveyDataDictMapper;

    /**
     * 查询勘测基础数据采集
     * @param id 勘测基础数据采集ID
     * @return 勘测基础数据采集
     */
    @Override
    public SurveyDataDto selectErpSurveyDataCollectInfoById (String id) {
        //1.查询基础数据
        SurveyBaseDataEntity surveyBaseDataEntity = erpSurveyDataCollectInfoMapper.selectErpSurveyDataCollectInfoById ( id );
        //2.查询字典数据
        SurveyDataDictEntity surveyDataDictEntity = new SurveyDataDictEntity ();
        surveyDataDictEntity.setBaseDataId ( id );
        List<SurveyDataDictEntity> surveyDataDictEntities = surveyDataDictMapper.selectErpSurveyDataDictInfoList ( surveyDataDictEntity );
        surveyBaseDataEntity.setDataDictEntities ( surveyDataDictEntities );
        //3.查询附件
        List<FileEntity> fileEntities = iFileInfoService.list ( new QueryWrapper<FileEntity> ().eq ( "relation_id", id ) );
        if ( fileEntities != null && fileEntities.size () > 0 ) {
            surveyBaseDataEntity.setFileEntities ( fileEntities );
        }
        //4.查询用电详情
        SurveyElectricEntity surveyElectricEntity = new SurveyElectricEntity ();
        surveyElectricEntity.setSurveyBaseDataId ( id );
        List<SurveyElectricEntity> surveyElectricEntities = surveyElectricMapper.selectErpSurveyElectricInfoList ( surveyElectricEntity );
        SurveyDataDto surveyDataDto = new SurveyDataDto ();
        surveyDataDto.setSurveyBaseDataEntity ( surveyBaseDataEntity );
        if ( surveyElectricEntities != null && surveyElectricEntities.size () > 0 ) {
            surveyDataDto.setSurveyElectricEntity ( surveyElectricEntities.get ( 0 ) );
        }
        return surveyDataDto;
    }

    /**
     * 查询勘测基础数据采集列表
     * @param erpSurveyDataCollectInfo 勘测基础数据采集
     * @return 勘测基础数据采集
     */
    @Override
    public List<SurveyBaseDataEntity> selectErpSurveyDataCollectInfoList (SurveyBaseDataEntity erpSurveyDataCollectInfo) {
        return erpSurveyDataCollectInfoMapper.selectErpSurveyDataCollectInfoList ( erpSurveyDataCollectInfo );
    }

    /**
     * 新增勘测基础数据采集
     * @param surveyDataDto 勘测基础数据采集
     * @return 结果
     */
    @Override
    @Transactional (propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int insertErpSurveyDataCollectInfo (SurveyDataDto surveyDataDto) {
        log.info ( "{}", surveyDataDto );
        //1.新增基础数据模块
        String baseDataId = SnowflakeIdWorker.getInstance ().nextStringId ();
        SurveyBaseDataEntity surveyBaseDataEntity = surveyDataDto.getSurveyBaseDataEntity ();
        surveyBaseDataEntity.setId ( baseDataId );
        surveyBaseDataEntity.setCreateTime ( DateUtils.getNowDate () );
        //2.新增用电情况
        SurveyElectricEntity surveyElectricEntity = surveyDataDto.getSurveyElectricEntity ();
        surveyElectricEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        surveyElectricEntity.setSurveyBaseDataId ( baseDataId );
        surveyElectricEntity.setCreateTime ( DateUtils.getNowDate () );
        int insert = surveyElectricMapper.insertErpSurveyElectricInfo ( surveyElectricEntity );
        if ( insert == 0 ) {
            throw new RuntimeException ( "勘测用电量情况插入失败" );
        }
        //3.处理基础资料中屋顶类型、厂房性质、屋面防水方式关联关系
        bindDict ( surveyBaseDataEntity.getDataDictEntities (), baseDataId );
        //4.处理基础数据模块中的附件，为已上传成功的fileId，更新这些file的relationId为当前基础数据Id，绑定关系
        updateFile ( surveyBaseDataEntity.getFileEntities (), baseDataId );
        return erpSurveyDataCollectInfoMapper.insertErpSurveyDataCollectInfo ( surveyBaseDataEntity );
    }

    /**
     * 修改勘测基础数据采集
     * @param surveyDataDto 勘测基础数据采集
     * @return 结果
     */
    @Override
    public int updateErpSurveyDataCollectInfo (SurveyDataDto surveyDataDto) {
        //1.修改基础数据
        SurveyBaseDataEntity surveyBaseDataEntity = surveyDataDto.getSurveyBaseDataEntity ();
        surveyBaseDataEntity.setChangeTime ( DateUtils.getNowDate () );
        //2.修改用电情况
        SurveyElectricEntity surveyElectricEntity = surveyDataDto.getSurveyElectricEntity ();
        surveyElectricEntity.setChangeTime ( DateUtils.getNowDate () );
        surveyElectricEntity.setSurveyBaseDataId ( surveyBaseDataEntity.getId () );
        int updateEle = surveyElectricMapper.updateSurveyElectricByBaseDataId ( surveyElectricEntity );
        //3.处理基础资料中屋顶类型、厂房性质、屋面防水方式关联关系
        bindDict ( surveyBaseDataEntity.getDataDictEntities (), surveyBaseDataEntity.getId () );
        //4.修改基础数据的附件
        updateFile ( surveyBaseDataEntity.getFileEntities (), surveyBaseDataEntity.getId () );
        return erpSurveyDataCollectInfoMapper.updateErpSurveyDataCollectInfo ( surveyBaseDataEntity );
    }

    /**
     * 批量删除勘测基础数据采集
     * @param ids 需要删除的勘测基础数据采集ID
     * @return 结果
     */
    @Override
    public int deleteErpSurveyDataCollectInfoByIds (String[] ids) {
        return erpSurveyDataCollectInfoMapper.deleteErpSurveyDataCollectInfoByIds ( ids );
    }

    /**
     * 删除勘测基础数据采集信息
     * @param id 勘测基础数据采集ID
     * @return 结果
     */
    @Override
    public int deleteErpSurveyDataCollectInfoById (String id) {
        return erpSurveyDataCollectInfoMapper.deleteErpSurveyDataCollectInfoById ( id );
    }

    /**
     * 修改勘测基础模块的附件，更新relationId为勘测记录Id
     * @param fileEntities
     * @param relationId
     */
    private void updateFile (List<FileEntity> fileEntities, String relationId) {
        if ( fileEntities != null && fileEntities.size () > 0 ) {
            fileEntities.forEach ( fileEntity -> {
                fileEntity.setRelationId ( relationId );
                fileEntity.setChangeTime ( DateUtils.getNowDate () );
            } );
            boolean b = iFileInfoService.updateBatchById ( fileEntities );
            if ( ! b ) {
                throw new RuntimeException ( "修改勘测基础数据附件失败" );
            }
        }
    }

    /**
     * 处理屋顶方基础数据与字典表的绑定
     * @param surveyDataDictEntities
     * @param baseDataId
     */
    private void bindDict (List<SurveyDataDictEntity> surveyDataDictEntities, String baseDataId) {
        if ( ! CollectionUtils.isEmpty ( surveyDataDictEntities ) ) {
            //先删除绑定关系
            surveyDataDictMapper.deleteErpSurveyDataDictInfoById ( baseDataId );
            for ( SurveyDataDictEntity surveyDataDictEntity : surveyDataDictEntities ) {
                surveyDataDictEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                surveyDataDictEntity.setBaseDataId ( baseDataId );
                surveyDataDictEntity.setCreateTime ( new Date () );
                int i = surveyDataDictMapper.insertErpSurveyDataDictInfo ( surveyDataDictEntity );
                if ( i == 0 ) {
                    throw new RuntimeException ( "新增屋顶方资料收集字典关系表" );
                }
            }
        }
    }

}
